/******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
* File Name          : main.c
* Author             : whq
* Version            : V2.0.1
* Date               : 20/02/2013
* Description        : Main program body
********************************************************************************
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/

/** @addtogroup IAP
  * @{
  */

/* Includes ------------------------------------------------------------------*/
#include "includes.h"

//#include <stdio.h>
//#include "common.h"
//#include "bsp.h"
//#include "cortexm3_macro.h"

//#include "download.h"
//#include "Port.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/


/* Private function prototypes -----------------------------------------------*/
#if 0

int TestAes(void)
{
    u8 j;
	uint8_t ret_text[16] = {0};
	uint8_t text[16] = {0};
	uint8_t text1[16*16] = {
		#if 1
		0x00,0x59,0x00,0x20,0x85,0x31,0x02,0x08,0xF9,0xD5,0x01,0x08,0xFB,0xD5,0x01,0x08,
		0xFD,0xD5,0x01,0x08,0xFF,0xD5,0x01,0x08,0x01,0xD6,0x01,0x08,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xD6,0x01,0x08,
		0x05,0xD6,0x01,0x08,0x00,0x00,0x00,0x00,0x07,0xD6,0x01,0x08,0x09,0xD6,0x01,0x08,
		0x71,0x33,0x02,0x08,0x75,0x33,0x02,0x08,0x79,0x33,0x02,0x08,0xED,0xDD,0x01,0x08,
		0x7D,0x33,0x02,0x08,0x81,0x33,0x02,0x08,0x85,0x33,0x02,0x08,0x89,0x33,0x02,0x08,
		0x45,0xDB,0x01,0x08,0x8D,0x33,0x02,0x08,0x91,0x33,0x02,0x08,0x95,0x33,0x02,0x08,
		0x99,0x33,0x02,0x08,0x9D,0x33,0x02,0x08,0xA1,0x33,0x02,0x08,0xA5,0x33,0x02,0x08,
		0xA9,0x33,0x02,0x08,0xAD,0x33,0x02,0x08,0xB1,0x33,0x02,0x08,0xB5,0x33,0x02,0x08,
		0x89,0xDD,0x01,0x08,0xB9,0x33,0x02,0x08,0xBD,0x33,0x02,0x08,0x35,0xDE,0x01,0x08,
		0xC1,0x33,0x02,0x08,0xC5,0x33,0x02,0x08,0xC9,0x33,0x02,0x08,0xCD,0x33,0x02,0x08,
		0x51,0xDA,0x01,0x08,0xD1,0x33,0x02,0x08,0xD5,0x33,0x02,0x08,0xD9,0x33,0x02,0x08,
		0xDD,0x33,0x02,0x08,0xE1,0x33,0x02,0x08,0xE5,0x33,0x02,0x08,0xE9,0x33,0x02,0x08,
		0xED,0x33,0x02,0x08,0x67,0xDB,0x01,0x08,0xA5,0xDB,0x01,0x08,0xFB,0xDB,0x01,0x08,
		0xF1,0x33,0x02,0x08,0x01,0xDE,0x01,0x08,0xF5,0x33,0x02,0x08,0x00,0x00,0x00,0x00,
		0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,



		#else
		0XFD,0xD5,0x01,0x08,0xFF,0xD5,0x01,0x08,0x01,0xD6,0x01,0x08,0x00,0x00,0x00,0x00
		#endif
	};
	uint8_t cipher_text[16] = {0};
	uint8_t key[32] = {
		#if 0
		0x0f,0x15,0x71,0xc9,
		0x47,0xd9,0xe8,0x59,
		0x0c,0xb7,0xad,0xd6,
		0xaf,0x7f,0x67,0x98,
		0x0f,0x15,0x71,0xc9,
		0x47,0xd9,0xe8,0x59,
		0x0c,0xb7,0xad,0xd6,
		0xaf,0x7f,0x67,0x98
		#endif
		0x9D, 0xD2, 0x00, 0x24, 0x84, 0x60, 0x2E, 0xDA,
	0x0C, 0xDD, 0x52, 0x7B, 0x05, 0xC1, 0x6B, 0x01,
	0xFF, 0x17, 0xCD, 0x6F, 0x8C, 0x1E, 0x3E, 0x09,
	0xCF, 0x1F, 0x0C, 0x78, 0x87, 0xEF, 0x8A, 0xEC
	};
	
	uint32_t key_bit[3] = {128, 192, 256};
	
	aes_context ctx;
	int i=2;
	//for (i = 0; i < sizeof(key_bit)/sizeof(key_bit[0]); ++i)
	{
		if (aes_set_key(&ctx, key, key_bit[i]) != SUCCESS)
		{
			printf("aes_set_key error.");
			return -1;
		}
		
	   for(i = 0;i < 16;i++)
	   	{
		   for (j = 0; j < 16; j++)
		   {
			   text[j] = text1[i * 16 + j];
			   printf("%02x ",text[j]);
		   }
		if(aes_encrypt_block(&ctx, cipher_text, text) != SUCCESS)
		{
			printf("aes_encrypt_block error.");
			return -1;
		}
		print("\tencrypt:  ", cipher_text);
	   	}
		if(aes_decrypt_block(&ctx, ret_text, cipher_text) != SUCCESS)
		{
			printf("aes_decrypt_block error.");
			return -1;
		}
		printf("key_bit %d: \n", key_bit[i]);
		print("\tinput  :  ", text);
		
		print("\tdecrypt:  ", ret_text);
		
		
		
		
	}
	return 0;
}
#endif

#if 0	
u8 *AesBufferIn;
u8 AesBufferOut[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

	uint8_t text1[8*16] = {
		0x09,0x5F,0xA7,0xAB,0x80,0xC5,0xE5,0x92,0xA9,0x7A,0x7F,0x6C,0xFA,0xF5,0x47,0x47,
		0x11,0x82,0x73,0x51,0x26,0x44,0x53,0x83,0x1D,0x18,0x41,0x1E,0xFF,0x16,0x68,0xF8,
		0x3A,0x3F,0x04,0x0F,0x5E,0x16,0xAF,0x49,0x65,0x84,0x43,0x8F,0x8E,0x68,0x2E,0x07,
		0xB6,0xBD,0xA2,0xF0,0x09,0xC0,0xA6,0xA4,0x5F,0x5D,0x0B,0x18,0xD1,0x30,0x36,0xEC,
		0x1F,0xD8,0x71,0x00,0x1E,0x4C,0xD5,0x99,0xC8,0x28,0xD7,0x20,0x25,0x06,0xEB,0x9B,
		0x83,0xE9,0x61,0x85,0x16,0x92,0x3B,0x87,0xC6,0x8A,0x9C,0x68,0xD0,0xF5,0xEF,0x7A,
		0x0F,0x71,0x2F,0x54,0x32,0x50,0x0C,0x13,0xF9,0x89,0x6A,0x03,0xD2,0xF3,0xEA,0x4A,
		0xE6,0x3A,0x54,0x76,0x7C,0x27,0xA1,0x26,0xF5,0x9C,0xFF,0x01,0x67,0xBF,0x69,0x58,
        };
	uint8_t text2[8*16] = {
		0xD5,0x5E,0xCB,0xBD,0xFF,0xBB,0x78,0x16,0x8F,0xE1,0x56,0x0C,0x8E,0xFE,0xD8,0xE4,
		0xC9,0xF1,0x9B,0x59,0x2A,0x59,0xDE,0x4D,0x36,0xC5,0x1B,0x8D,0x04,0x69,0xCD,0x9D,
		0x27,0xC4,0xD4,0xB5,0x9C,0xC9,0x90,0x22,0x1A,0xCB,0x05,0xF7,0x53,0x15,0x37,0xDE,
		0x4A,0x04,0x1D,0x14,0x42,0x2A,0xED,0xEC,0x95,0xAD,0x06,0x8A,0x59,0x7F,0x0E,0xB4,
		0x04,0x0A,0xF2,0x32,0x86,0x2B,0xF9,0x94,0x16,0x90,0x89,0xFC,0xE3,0x38,0xCE,0x92,
		0xAB,0x61,0x63,0xAB,0xAE,0x90,0x7E,0xEB,0x59,0x7E,0x65,0x45,0x54,0x1E,0xE5,0xF4,
		0xEA,0xD0,0x06,0xBA,0xBE,0xA4,0x81,0xE4,0xDD,0x3F,0x9F,0x13,0xBA,0x4B,0x55,0xB4,
		0xB4,0x78,0x5A,0xB9,0x5C,0x51,0xA7,0x48,0x57,0x2E,0x20,0x09,0x4B,0xA5,0x32,0x5F,
	};

int TestAes(void)
{
	
	u8 j,i;
	
	
	AesBufferIn = text1;
	aesDecInit();
	printf("\r\nin");
	for(j = 0;j < 128;j += 16)
	{
		for(i = 0;i < 16 ;i++)
			printf("%02x ",AesBufferIn[i]);
		aesDecrypt(AesBufferIn,AesBufferOut);
		AesBufferIn+=16;
			
	}
	AesBufferIn = text2;
	aesDecInit();
	printf("\r\nin");
	for(j = 0;j < 128;j += 16)
	{
		for(i = 0;i < 16 ;i++)
			printf("%02x ",AesBufferIn[i]);
		aesDecrypt(AesBufferIn,AesBufferOut);
		AesBufferIn+=16;
			
	}
	
}
#endif
void Main_SysTick_Config(void)
{
    SysTick_Config(SystemCoreClock / 1000);

}

int fputc(int ch, FILE *f)
{   
	USART_ClearFlag(UART4,USART_FLAG_TC);
    USART_SendData(UART4,(uint8_t)ch);
	while(USART_GetFlagStatus(UART4,USART_FLAG_TC)==RESET){};
    return ch;              
}
void show_welcome_message(void)
{ 
  printf("\r\n*****************************************************************");
  printf("\r\nLF MBAndroid MMMmcuBootloader Build at:");
  printf("%s %s",__DATE__,__TIME__);  
  printf("\r\n*****************************************************************\r\n\r\n");
}

void Show_ResetMode(void)
{
	printf("\r\nRCC_FLAG_PINRST is:%d",RCC_GetFlagStatus(RCC_FLAG_PINRST));
	printf("\r\nRCC_FLAG_SFTRST is:%d",RCC_GetFlagStatus(RCC_FLAG_SFTRST));
	printf("\r\nRCC_FLAG_IWDGRST is:%d",RCC_GetFlagStatus(RCC_FLAG_IWDGRST));
	printf("\r\nRCC_FLAG_WWDGRST is:%d",RCC_GetFlagStatus(RCC_FLAG_WWDGRST));
	printf("\r\nRCC_FLAG_LPWRRST is:%d",RCC_GetFlagStatus(RCC_FLAG_LPWRRST));
	
	RCC_ClearFlag();
	printf("\r\n");
}


/*******************************************************************************
* Function Name  : main
* Description    : Main program.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
int main(void)
{
	//u32 tempfalg = UPDATA_VALUE;
	
    BspInit();	
	
	Main_SysTick_Config();
	Acc_Wdg_Config();	//watch dog
	//SYS_PowerOn();
	TFT_LightOff();
	TFT_Light_PWM_Off();
	UpgradeReady = 0;
	Tw8836InitFlag = 0;
	A100_IN_PORT_RESET();
	show_welcome_message();
	Show_ResetMode();
	//aesDecInit();
	//TestAes();
    while (1)
    {
		CommonExec();	
	#ifdef ANDROID_UPGRADE
		DealNAVIComMag();
		//Upgrade_CMD_PROCESS();
	#else
		MApp_Proc_Uart();
	#endif
    }
}

#ifdef  DEBUG
/*******************************************************************************
* Function Name  : assert_failed
* Description    : Reports the name of the source file and the source line number
*                  where the assert_param error has occurred.
* Input          : - file: pointer to the source file name
*                  - line: assert_param error line source number
* Output         : None
* Return         : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{ 
  /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif





/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
